<?php

namespace app\common\model;

use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog extends Model
{

    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];

    public static function addMoney($userId,$money,$remark)
    {
        $userDetail = User::find($userId);
        if(empty($userDetail)) return false;
        self::insert(['user_id' => $userId,'money' => $money,'before' => $userDetail->money,'after' =>  $userDetail->money + $money,'memo' => $remark,'createtime' => time()]); 
        $userDetail->money += $money;
        return $userDetail->save();
    }
}
